package qdu.rg012.ch4_data_contntprvdr;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import android.util.Log;

public class Mydbapt {
    //---------------成员变量------------------------------
    private static String DB_NAME = "testctntp.db";
    private static String TABLE_NAME = "mydata";
    private static int DB_VERSION = 1;

    private static String KEY_ID = "_id";
    private static String KEY_DATA = "msg";

    Mydbhelper mydbhelper;
    private Context c;
    private SQLiteDatabase db;
    //----------------构造/成员函数-------------------------
    public Mydbapt(Context c) {
        this.c = c;
        mydbhelper = new Mydbhelper(c, DB_NAME, null, DB_VERSION);
    }

    public Cursor getall(){
        if(db != null && db.isOpen()){
            return db.query(TABLE_NAME,null,null,null,null,null,null);
        }else
            return null;
    }
    public Cursor getitembyid(String id){
        Log.i("mytag", "db get by id:" + id);
        if(db != null && db.isOpen()){
            Log.i("mytag", "db get by id");
            return db.query(TABLE_NAME, null, KEY_ID + " = " + id, null, null,null,null);
        }else
            return null;
    }
    public long insertitem(ContentValues cv){
        long newid = -1;
        if(db != null && db.isOpen()){
            newid = db.insert(TABLE_NAME, null, cv);
        }
        return newid;
    }
    public boolean opendb(){
        if(mydbhelper != null){
            try {
                db = mydbhelper.getWritableDatabase();
            }catch (SQLiteException e){
                try {
                    db = mydbhelper.getReadableDatabase();
                }catch(SQLiteException e2){
                    Log.i("mytag", "can not connect to db....");
                    e2.printStackTrace();
                }
            }
        }
        if(db != null && db.isOpen())
            return true;
        else
            return false;
    }
    public void closedb(){
        if(mydbhelper != null && db != null && db.isOpen()){
            mydbhelper.close();
            db.close();
        }
    }
    public boolean dbisopen(){
        if(db != null && db.isOpen())
            return true;
        else
            return false;
    }
    //----------------内部类-------------------------
    class Mydbhelper extends SQLiteOpenHelper{

        public Mydbhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sqlstr = "";
            if(db !=null && db.isOpen()){
                sqlstr = "create table " + TABLE_NAME + " ( " +
                         KEY_ID + " integer primary key autoincrement, " +
                        KEY_DATA + " text);";
                try {
                    db.execSQL(sqlstr);
                }catch (SQLiteException e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }

}
